{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "import lightgbm as lgb\n",
    "import numpy as np\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import accuracy_score\n",
    "import time\n",
    "from concurrent.futures import ThreadPoolExecutor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_all_data = pd.read_hdf('data/train_transform.h5')\n",
    "test_all_data = pd.read_hdf('data/test_transform.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = train_all_data.drop('type', axis=1)\n",
    "y_train = train_all_data[\"type\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "训练"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "params = {\n",
    "    'learning_rate': 0.01,\n",
    "    'min_child_samples': 5,\n",
    "    'max_depth': 7,\n",
    "    'lambda_l1': 2,\n",
    "    'boosting': 'gbdt',\n",
    "    'objective': 'multiclass',\n",
    "    'n_estimators': 2000,\n",
    "    'metric': 'multi_error',\n",
    "    'num_class': 3,\n",
    "    'feature_fraction': .75,\n",
    "    'bagging_fraction': .85,\n",
    "    'seed': 99,\n",
    "    'num_threads': 20,\n",
    "    'verbose': -1\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def train(models,oof):\n",
    "    fold = StratifiedKFold(n_splits=3, shuffle=True, random_state=42)\n",
    "\n",
    "    for index, (train_idx, val_idx) in enumerate(fold.split(X_train, y_train)):\n",
    "        x_tr = X_train.iloc[train_idx]; x_te = X_train.iloc[val_idx]\n",
    "        y_tr = y_train.iloc[train_idx]; y_te = y_train.iloc[val_idx]\n",
    "\n",
    "        clf = lgb.LGBMClassifier(**params)\n",
    "        clf.fit(x_tr, y_tr, eval_set = [(x_te, y_te)], early_stopping_rounds = 500, verbose = False)\n",
    "\n",
    "        models.append(clf)\n",
    "        val_pred = clf.predict_proba(x_te)\n",
    "        oof[val_idx] += val_pred\n",
    "        val_pred = np.argmax(val_pred, axis=1)\n",
    "        print(index, 'f1', metrics.f1_score(y_te, val_pred, average='macro'))\n",
    "        print('accuracy', accuracy_score(y_te,val_pred))\n",
    "    return models,oof"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/carl-hui/.virtualenvs/AI/lib/python3.7/site-packages/ipykernel_launcher.py:1: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 f1 0.8236946386685663\n",
      "accuracy 0.8676092544987146\n",
      "1 f1 0.8305661325560668\n",
      "accuracy 0.8705529361337334\n",
      "2 f1 0.8459988852701995\n",
      "accuracy 0.8795542220317188\n",
      "710.5992090000001\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/carl-hui/.virtualenvs/AI/lib/python3.7/site-packages/ipykernel_launcher.py:7: DeprecationWarning: time.clock has been deprecated in Python 3.3 and will be removed from Python 3.8: use time.perf_counter or time.process_time instead\n",
      "  import sys\n"
     ]
    }
   ],
   "source": [
    "start = time.clock()\n",
    "models = []\n",
    "oof = np.zeros((len(X_train), 3))\n",
    "# with ThreadPoolExecutor(max_workers=5) as t: \n",
    "#     f1 = t.submit(train,models,oof)\n",
    "#     models,oof = f1.result()\n",
    "models,oof = train(models,oof)\n",
    "end = time.clock()\n",
    "print(str(end-start))  #704.373854"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "oof f1 0.8334532490682224\n",
      "train accuracy 0.8725714285714286\n"
     ]
    }
   ],
   "source": [
    "oof = np.argmax(oof, axis=1)\n",
    "print('oof f1', metrics.f1_score(y_train,oof, average='macro'))\n",
    "print('train accuracy', accuracy_score(y_train,oof))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "生产"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getpred(test_data):\n",
    "    pred = np.zeros((len(test_data),3))\n",
    "    for model in models:\n",
    "        test_pred = model.predict_proba(test_data)\n",
    "        pred += test_pred\n",
    "    pred = np.argmax(pred, axis=1)\n",
    "    return pred"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "sub = test_all_data[['ship']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "production_pred = getpred(test_all_data.drop('ship', axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2    0.5260\n",
      "0    0.2905\n",
      "1    0.1835\n",
      "Name: pred, dtype: float64\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/carl-hui/.virtualenvs/AI/lib/python3.7/site-packages/ipykernel_launcher.py:1: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  \"\"\"Entry point for launching an IPython kernel.\n",
      "/home/carl-hui/.virtualenvs/AI/lib/python3.7/site-packages/ipykernel_launcher.py:4: SettingWithCopyWarning: \n",
      "A value is trying to be set on a copy of a slice from a DataFrame.\n",
      "Try using .loc[row_indexer,col_indexer] = value instead\n",
      "\n",
      "See the caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
      "  after removing the cwd from sys.path.\n"
     ]
    }
   ],
   "source": [
    "sub['pred'] = production_pred\n",
    "\n",
    "print(sub['pred'].value_counts(1))\n",
    "sub['pred'] = sub['pred'].map({0:'围网',1:'刺网',2:'拖网'})\n",
    "sub.to_csv('data/result.csv', index=None, header=None)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
